package liaotian;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ChatServer {
	private ExecutorService pool;
	private ServerSocket svr;
	private Socket socket;	
	private ChatRecord<String> chatRecord;
	private List<Socket> sockets;
	public ChatServer() {
		sockets=new ArrayList<>();
		chatRecord=new ChatRecord<>();
		/*chatRecord.addPropChangeListener(new PropertyChangeListener() {
			
			@Override
			public void propertyChange(PropertyChangeEvent evt) {
				System.out.println("****"+sockets.size()+evt.getPropertyName()+" "+evt.getNewValue()+" "+evt.getOldValue());
				for (Socket socket : sockets) {
					try {
						ObjectOutputStream oos=new ObjectOutputStream(socket.getOutputStream());
						oos.writeObject(evt.getNewValue());
						oos.flush();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
		});*/
		try {
			pool=Executors.newFixedThreadPool(5);
			svr=new ServerSocket(9988);
			System.out.println("服务器启动");	
			while(true){
				socket=svr.accept();
				sockets.add(socket);
				pool.execute(new ServerReceive(socket, chatRecord,sockets));
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	public static void main(String[] args) {
		new ChatServer();
	}
}
